<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'phone','promo_code','parent_id','status'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 手机号登录注册验证
     * @param $phone
     * @param string $promo_code
     * @return mixed
     */
    static public function phoneUser($phone, $promo_code='')
    {
        $user = self::where('phone', $phone)->first();
        //账号是否存在
        if (!$user) {
            //是否有推荐码
            $parent_id = 0;
            if ($promo_code) {
                //查询推荐人id
                $promo = self::where('promo_code', $promo_code)->where('parent_id', 0)->first();
                //推荐人id
                $parent_id = $promo->id;
            }
            //生成唯一的推荐码
            $Uuid = \Uuid::generate();
            //创建新用户
            $user = self::create([
                'phone' => $phone,
                'promo_code' => $Uuid,
                'parent_id' => $parent_id,
                'status' => NORMAL_USER,
            ]);
            return $user;
        }
        return $user;
    }

    /**
     * 筛选手机账号
     * @param $query
     * @param $phone
     */
    public function scopePhone($query,$phone)
    {
        if($phone){
            $query->where('phone','like','%'.$phone.'%');
        }
    }
    /**
     * 筛选手机账号
     * @param $query
     * @param $name
     */
    public function scopeName($query,$name)
    {
        if($name){
            $query->where('name','like','%'.$name.'%');
        }
    }

    /**
     * 排序
     * @param $query
     * @param $sort
     */
    public function  scopeSort($query,$sort)
    {
        //倒叙
        if($sort==0){
            $query->OrderBy('user_id','desc');
        }
        //交易额
        if($sort == 1){
            $query->OrderBy('order_price','desc');
        }
        //交易量 paimai.test.hanguosoft.com
        if($sort == 2){
            $query->OrderBy('order_count','desc');
        }
    }

    /**
     * 编辑
     * @param $user_id
     * @param $name
     * @param $head
     * @return bool
     */
    static  public function  saveUser($user_id,$name,$head)
    {
        $user = self::find($user_id);
        $user->name = $name;
        $user->head = $head;
        if($user->save()){
            return true;
        }
        return false;
    }

    /**
     * 冻结/解冻用户
     * @param $user_id
     * @param $status
     * @return bool
     */
   static public function freeze($user_id)
   {
       $user =  self::find($user_id);
       if($user->status == 1){
           $status =2;
       }
       if($user->status == 2){
           $status =1;
       }
       $user->status = $status;
       if($user->save()){
           return true;
       }
       return false;

   }

    /**
     * 用户认证-买家
     * @return $this
     */
   public function  buyers()
   {
       return $this->hasMany(UserBuyer::class,'user_id','id');
   }
    /**
     * 用户认证-卖家
     * @return $this
     */
    public function  seller()
    {
        return $this->hasMany(UserSeller::class,'user_id','id');
    }



}
